﻿#region License


// Virtual Essentials - http://www.Virtual-Essentials.com
// Copyright (c) 2005-2013
// 
//  This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#endregion
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;

using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Roles;
using DotNetNuke.Web.Api;
using DotNetNuke.Instrumentation;
using DotNetNuke.Security;

using VirtualEssentials.DNN.Modules.Testimonials;

namespace VirtualEssentials.DNN.Modules.Testimonials.Services
{


    /// <summary>
    /// Creates web api for controller
    /// </summary>
    [DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.View)]
    [ValidateAntiForgeryToken]
    public class TestimonialWebController: DnnApiController
    {
        private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(TestimonialWebController));
        DataProvider data = new DataProvider();
        #region DTOs
        public class TestimonialInfoDTO
        {
            public int TestimonialID { get; set; }
            public bool IsPublished { get; set; }
        }
        #endregion


        /// <summary>
        /// Returns a list of published testimonials
        /// </summary>
        /// <returns></returns>
        //[DnnAuthorize]
        [AllowAnonymous]
        [HttpGet]
        public HttpResponseMessage GetTestimonials()
        {
            try
            {
                
                //Controller.TestimonialController objController = new Controller.TestimonialController();
                //how to get current context ModuleId
                DotNetNuke.Entities.Modules.ModuleInfo activeModule = this.ActiveModule;
                //List<TestimonialInfo> lstTestimonials = objController.GetTestimonials(activeModule.ModuleID);
                List<TestimonialInfo> lstTestimonials = (List<TestimonialInfo>)data.GetTestimonials(activeModule.ModuleID);
                return Request.CreateResponse(HttpStatusCode.OK, lstTestimonials.ToList());
            }
            catch (System.Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }
        }

        /// <summary>
        /// Publishes a Testimonial (or Unpublishes a Testimonial)
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        [DnnAuthorize]
        [DnnModuleAuthorize(AccessLevel = SecurityAccessLevel.Edit)]
        public System.Net.Http.HttpResponseMessage UpdateTestimonial(TestimonialInfoDTO item)
        {
            try
            {
                //Controller.TestimonialController objController = new Controller.TestimonialController();
                data.PublishTestimonial(item.TestimonialID, item.IsPublished);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (System.Exception exc)
            {
                Logger.Error(exc);
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
            }

        }



        /// <summary>
        /// Publishes a Testimonial (or Unpublishes a Testimonial)
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        [DnnModuleAuthorize(PermissionKey="PUBLISHTESTIMONIAL")]
        public HttpResponseMessage DeleteTestimonial(TestimonialInfoDTO item)
        {
            //Controller.TestimonialController objController = new Controller.TestimonialController();
            try
            {
                data.DeleteTestimonial(item.TestimonialID);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (HttpResponseException ex)
            {
                Logger.Error(ex);
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

       ///// <summary>
       // /// Test Unit for Published Testimonials
       // /// </summary>
       // /// <returns></returns>
       // [AllowAnonymous]
       // [HttpGet]
       // public HttpResponseMessage TEST_GetPublishedTestimonials()
       // {
       //     //Controller.TestimonialController objController = new Controller.TestimonialController();
       //     List<TestimonialInfo> lstTestimonials = objController.GetPublishedTestimonials(249);
       //     return Request.CreateResponse(HttpStatusCode.OK, lstTestimonials.ToList());
       // }
    }
}
